#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Purpose: SDDP-Aquaplan object types definition
# Created: 07/06/2009
# $Id $
# Copyright (c) 2010, Unesco-IHE
# Authors: 
#  Didrik Pinte --<dpinte@dipole-consulting.com>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with HydroPlatform for details.

import logging

from hydroplatform.model import network
from hydroplatform.model.fields import Parameter, TimeSerie, Table, \
     SeasonalParameter, NodeReferences, LinkReferences, TableCol

from hydroplatform.model import units


Column =  TableCol

def is_aquaplan_project(project):
    """
    Check if the given project already has the needed object types
    """
    return False

RESERVOIR_NODE = u"Reservoir"
IRRIGATION_NODE = u"Irrigation"
HYDRO_NODE = u"Hydro"
CROP_NODE = u"Crop"

SPILLAGE_LINK = u"Spillage"
RELEASE_LINK = u"Release"
SP_RE_LINK = u"Spillage/Release"
IRRIGATION_LINK = u"Irrigation"
RETURNFLOW_LINK = u"Irrigation return flow"

def get_node_types(project):
    node_key_names = [u"name", u"description", u"size", u"shape", u"pen", u"brush", "visible_on_map"]

    # FIXME : crop definition has no visual representation ...
    
    reservoir_fields = [[NodeReferences(name=u"hydro", single=True), u"Hpp"],
                        [Parameter(name=u"smin", unit=u"hm3", format="integer"), u"Hpp"],
                        [Parameter(name=u"smax", unit=u"hm3", format="integer"), u"Hpp"],
                        [Parameter(name=u"rmin", unit=u"m3/s", format="integer"), u"Hpp"],
                        [Parameter(name=u"rmax", unit=u"m3/s", format="integer"), u"Hpp"],
                        [Parameter(name=u"rho", unit=u"-"), u"Hpp"],
                        [Parameter(name=u"capacity", unit=u"MW"), u"Hpp"],
                        [Parameter(name=u"spill_penalty", unit=u"$/m3"), u"Hpp"],
                        [Parameter(name=u"penalty_for_low_storage", unit=u"$/m3"), u"Hpp"],
                        [Parameter(name=u"storage_for_penalty", unit=u"$/m3"), u"Hpp"],
                        SeasonalParameter(name=u"energy_price",  
                                  unit=u"$/MWh", 
                                  fromd = project.horizon_from, 
                                  tod = project.horizon_to,
                                  step = units.MONTHLY,
                                  horizon = True),
                        SeasonalParameter(name=u"evaporation",  
                                  unit=u"mm/month", 
                                  fromd=project.horizon_from, 
                                  tod = project.horizon_to,
                                  step = units.MONTHLY,
                                  horizon=True),
                        Table(name=u"reservoir", 
                              columns=[Column(name=u"Head", unit=u"m"),
                                       Column(name=u"Surface", unit=u"km2"),
                                       Column(name=u"Storage", unit=u"hm3")
                                       ])
                        ]
    
    hydro_fields = [TimeSerie(name=u"hydrology",  
                                  unit=u"m3/s", 
                                  fromd=project.horizon_from, 
                                  tod = project.horizon_to,
                                  step = units.MONTHLY,
                                  horizon=False)]
    
    crop_fields = [Parameter(name=u"yield", unit=u"T/ha"),
                   Parameter(name=u"crop_price", unit=u"$/T"),
                   Parameter(name=u"O&M_costs", unit=u"$/ha"),
                   Parameter(name=u"return_date", unit=u"month", format=u"integer"),
                   Parameter(name=u"ky", unit=u"-"),
                   Parameter(name=u"first_irrigation_month", unit=u"month", format=u"integer"),
                   SeasonalParameter(name=u"water_need",  
                                  unit=u"m3/ha", 
                                  fromd=project.horizon_from, 
                                  tod = project.horizon_to,
                                  step = units.MONTHLY,
                                  horizon=True)
                    ]
    
    irrigation_fields = [NodeReferences(name=u"crop", class_filter=u"Crop",\
                                        single=True),
                         Parameter(name=u"surface", unit=u"ha", format=u"integer"),
                         Parameter(name=u"efficiency", unit=u"-"),
                         Parameter(name=u"return_efficiency", unit=u"-"),
                         Parameter(name=u"irrigation_penalty", unit=u"$/hm3")
                         ]
    
    node_types = [[RESERVOIR_NODE, u"Reservoirs", 
                   u"triangle", u"#000000", u"#0000ff", 20, True, reservoir_fields],
                  [IRRIGATION_NODE, u"Irrigation area's", 
                   u"rectangle", u"#000000", u"#008000", 20, True, irrigation_fields],
                  [HYDRO_NODE, u"Hydrological measure point", 
                   u"circle", u"blue", u"blue", 10, True, hydro_fields],
                  [CROP_NODE, u"Available crops", u"rectangle", u"black", \
                   u"black", 10, False, crop_fields]
                  ]
    
    return node_key_names, node_types

def get_link_types(project):
    link_key_names = ["name", "description", "size", "pen", "style"]
    
    link_types = [[SPILLAGE_LINK, u"Link where the spillage is sent", []], 
                  [RELEASE_LINK, u"Link where the release is sent", []],
                  [SP_RE_LINK, u"Link with both release and spillage", []],
                  [IRRIGATION_LINK, u"Links reservoirs and irrigation area's", []],
                  [RETURNFLOW_LINK, u"Links retrurn flow's from irrigation area's to reservoir", []]
                 ]
    return link_key_names, link_types